home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH11 / SRC / OBJTRANS.CLS < prev    next >
Text File  |  1996-05-04  |  10KB  |  362 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "ObjTransformed"
  6. Attribute VB_Creatable = False
  7. Attribute VB_Exposed = False
  8. Option Explicit
  9.  
  10. Private NumCurvePts As Integer
  11. Private CurvePoints() As Point3D
  12.  
  13. Private NumTrans As Integer
  14. Private trans() As Transformation
  15.  
  16. Private solid As ObjSolid   ' The display solid.
  17. ' ************************************************
  18. ' Add a point to the curve.
  19. ' ************************************************
  20. Public Sub AddCurvePoint(x As Single, Y As Single, z As Single)
  21.     NumCurvePts = NumCurvePts + 1
  22.     ReDim Preserve CurvePoints(1 To NumCurvePts)
  23.     CurvePoints(NumCurvePts).coord(1) = x
  24.     CurvePoints(NumCurvePts).coord(2) = Y
  25.     CurvePoints(NumCurvePts).coord(3) = z
  26.     CurvePoints(NumCurvePts).coord(4) = 1
  27. End Sub
  28.  
  29.  
  30.  
  31.  
  32. ' ************************************************
  33. ' Set a transformation.
  34. ' ************************************************
  35. Public Sub SetTrans(M() As Single)
  36.     NumTrans = NumTrans + 1
  37.     ReDim Preserve trans(1 To NumTrans)
  38.     m3MatCopy trans(NumTrans).M, M
  39. End Sub
  40.  
  41. ' ************************************************
  42. ' Return the solid's Zmax value.
  43. ' ************************************************
  44. Public Function zmax() As Single
  45.     If solid Is Nothing Then
  46.         zmax = -INFINITY
  47.     Else
  48.         zmax = solid.zmax
  49.     End If
  50. End Function
  51. ' ************************************************
  52. ' Make the solid compute its Zmax value.
  53. ' ************************************************
  54. Public Sub SetZmax()
  55.     If Not solid Is Nothing Then solid.SetZmax
  56. End Sub
  57.  
  58.  
  59. ' ************************************************
  60. ' Create the display solid by applying the
  61. ' series of transformations in array M().
  62. ' ************************************************
  63. Public Sub Transform(Optional cap_ends As Variant)
  64. Dim pgon As ObjPolygon
  65. Dim i As Integer
  66. Dim j As Integer
  67. Dim x0 As Single
  68. Dim y0 As Single
  69. Dim z0 As Single
  70. Dim x1 As Single
  71. Dim y1 As Single
  72. Dim z1 As Single
  73. Dim x2 As Single
  74. Dim y2 As Single
  75. Dim z2 As Single
  76. Dim x3 As Single
  77. Dim y3 As Single
  78. Dim z3 As Single
  79.  
  80.     If IsMissing(cap_ends) Then cap_ends = True
  81.     
  82.     Set solid = New ObjSolid
  83.         
  84.     ' Add the base curve to solid assuming the
  85.     ' curve is stored oriented towards the
  86.     ' transformations.
  87.     If cap_ends Then
  88.         Set pgon = New ObjPolygon
  89.         solid.AddPolygon pgon
  90.         For i = NumCurvePts - 1 To 1 Step -1
  91.             pgon.AddPoint _
  92.                 CurvePoints(i).coord(1), _
  93.                 CurvePoints(i).coord(2), _
  94.                 CurvePoints(i).coord(3)
  95.         Next i
  96.     End If
  97.     
  98.     ' Start with the transformed coordinates
  99.     ' the same as the original coordinates.
  100.     For i = 1 To NumCurvePts
  101.         CurvePoints(i).trans(1) = CurvePoints(i).coord(1)
  102.         CurvePoints(i).trans(2) = CurvePoints(i).coord(2)
  103.         CurvePoints(i).trans(3) = CurvePoints(i).coord(3)
  104.     Next i
  105.     
  106.     ' Create the transformed copies of the curve.
  107.     For i = 1 To NumTrans
  108.         x0 = CurvePoints(1).trans(1)
  109.         y0 = CurvePoints(1).trans(2)
  110.         z0 = CurvePoints(1).trans(3)
  111.         m3ApplyFull _
  112.             CurvePoints(1).coord, trans(i).M, _
  113.             CurvePoints(1).trans
  114.         x1 = CurvePoints(1).trans(1)
  115.         y1 = CurvePoints(1).trans(2)
  116.         z1 = CurvePoints(1).trans(3)
  117.         
  118.         For j = 2 To NumCurvePts
  119.             x2 = CurvePoints(j).trans(1)
  120.             y2 = CurvePoints(j).trans(2)
  121.             z2 = CurvePoints(j).trans(3)
  122.             m3ApplyFull _
  123.                 CurvePoints(j).coord, trans(i).M, _
  124.                 CurvePoints(j).trans
  125.             x3 = CurvePoints(j).trans(1)
  126.             y3 = CurvePoints(j).trans(2)
  127.             z3 = CurvePoints(j).trans(3)
  128.             
  129.             solid.AddFace _
  130.                 x0, y0, z0, _
  131.                 x2, y2, z2, _
  132.                 x1, y1, z1
  133.             
  134.             solid.AddFace _
  135.                 x2, y2, z2, _
  136.                 x3, y3, z3, _
  137.                 x1, y1, z1
  138.             x0 = x2
  139.             y0 = y2
  140.             z0 = z2
  141.             x1 = x3
  142.             y1 = y3
  143.             z1 = z3
  144.         Next j
  145.     Next i
  146.  
  147.     ' Add the final curve to solid assuming
  148.     ' the curve is stored oriented towards the
  149.     ' transformations.
  150.     If cap_ends Then
  151.         Set pgon = New ObjPolygon
  152.         solid.AddPolygon pgon
  153.         For i = 2 To NumCurvePts
  154.             pgon.AddPoint _
  155.                 CurvePoints(i).trans(1), _
  156.                 CurvePoints(i).trans(2), _
  157.                 CurvePoints(i).trans(3)
  158.         Next i
  159.     End If
  160. End Sub
  161.  
  162. ' ***********************************************
  163. ' Return a string indicating the object type.
  164. ' ***********************************************
  165. Property Get ObjectType() As String
  166.     ObjectType = "TRANSFORMED"
  167. End Property
  168.  
  169.  
  170.  
  171. ' ***********************************************
  172. ' Fix the data coordinates at their transformed
  173. ' values.
  174. ' ***********************************************
  175. Public Sub FixPoints()
  176. Dim i As Integer
  177. Dim j As Integer
  178.  
  179.     ' Fix the curve points.
  180.     For i = 1 To NumCurvePts
  181.         For j = 1 To 3
  182.             CurvePoints(i).coord(j) = CurvePoints(i).trans(j)
  183.         Next j
  184.     Next i
  185.  
  186.     ' Fix the display solid if it exists.
  187.     If Not solid Is Nothing Then solid.FixPoints
  188. End Sub
  189.  
  190. ' ************************************************
  191. ' Apply a transformation matrix which may not
  192. ' contain 0, 0, 0, 1 in the last column to the
  193. ' object.
  194. ' ************************************************
  195. Public Sub ApplyFull(M() As Single)
  196. Dim i As Integer
  197.  
  198.     ' Transform the curve.
  199.     For i = 1 To NumCurvePts
  200.         m3ApplyFull CurvePoints(i).coord, M, _
  201.                     CurvePoints(i).trans
  202.     Next i
  203.     
  204.     ' Transform the display solid if it exists.
  205.     If Not solid Is Nothing Then solid.ApplyFull M
  206. End Sub
  207.  
  208. ' ************************************************
  209. ' Apply a transformation matrix to the object.
  210. ' ************************************************
  211. Public Sub Apply(M() As Single)
  212. Dim i As Integer
  213.  
  214.     ' Transform the curve.
  215.     For i = 1 To NumCurvePts
  216.         m3Apply CurvePoints(i).coord, M, _
  217.                 CurvePoints(i).trans
  218.     Next i
  219.     
  220.     ' Transform the display solid if it exists.
  221.     If Not solid Is Nothing Then solid.Apply M
  222. End Sub
  223.  
  224.  
  225. ' ************************************************
  226. ' Apply a nonlinear transformation.
  227. ' ************************************************
  228. Public Sub Distort(D As Object)
  229. Dim i As Integer
  230.  
  231.     ' Distort the curve.
  232.     For i = 1 To NumCurvePts
  233.         D.Distort CurvePoints(i).coord(1), _
  234.                   CurvePoints(i).coord(2), _
  235.                   CurvePoints(i).coord(3)
  236.     Next i
  237.     
  238.     ' Distort the display solid if it exists.
  239.     If Not solid Is Nothing Then solid.Distort D
  240. End Sub
  241.  
  242.  
  243. ' ************************************************
  244. ' Write the surface's display solid object to a
  245. ' file using Write. The data can later be loaded
  246. ' into an Objsolid object but not an
  247. ' ObjRotated object.
  248. ' ************************************************
  249. Public Sub FileWriteSolid(filenum As Integer)
  250. Dim M(1 To 4, 1 To 4) As Single
  251.     
  252.     
  253.     If Not solid Is Nothing Then solid.FileWrite filenum
  254. End Sub
  255.  
  256.  
  257. ' ************************************************
  258. ' Write an extruded surface to a file using Write.
  259. ' Begin with "TRANSFORMED" to identify this object.
  260. ' ************************************************
  261. Public Sub FileWrite(filenum As Integer)
  262. Dim i As Integer
  263. Dim j As Integer
  264. Dim k As Integer
  265.  
  266.     ' Write basic information.
  267.     Write #filenum, "TRANSFORMED", NumCurvePts, _
  268.         NumTrans
  269.         
  270.     ' Write the curve points.
  271.     For i = 1 To NumCurvePts
  272.         Write #filenum, _
  273.             CurvePoints(i).coord(1), _
  274.             CurvePoints(i).coord(2), _
  275.             CurvePoints(i).coord(3)
  276.     Next i
  277.     
  278.     ' Write the transformations.
  279.     For i = 1 To NumTrans
  280.         For j = 1 To 4
  281.             For k = 1 To 4
  282.                 Write #filenum, trans(i).M(j, k)
  283.             Next k
  284.         Next j
  285.     Next i
  286. End Sub
  287.  
  288.  
  289.  
  290.  
  291. ' ************************************************
  292. ' Draw the extrusion on a Form, Printer, or
  293. ' PictureBox.
  294. ' ************************************************
  295. Public Sub Draw(canvas As Object, Optional r As Variant)
  296.     If Not solid Is Nothing Then _
  297.         solid.Draw canvas, r
  298. End Sub
  299.  
  300.  
  301. ' ************************************************
  302. ' Perform backface removal on the display solid.
  303. ' ************************************************
  304. Public Sub Cull(x As Single, Y As Single, z As Single)
  305.     If Not solid Is Nothing Then solid.Cull x, Y, z
  306. End Sub
  307.  
  308. ' ***********************************************
  309. ' Set or clear the Culled property for the solid.
  310. ' ***********************************************
  311. Property Let Culled(value As Boolean)
  312.     If Not solid Is Nothing Then solid.Culled = value
  313. End Property
  314. ' ************************************************
  315. ' Draw the transformed display solid on a Form,
  316. ' Printer, or PictureBox. Draw the faces in
  317. ' depth-sort order.
  318. ' ************************************************
  319. Public Sub DrawOrdered(canvas As Object, Optional r As Variant)
  320.     If Not solid Is Nothing Then solid.DrawOrdered canvas, r
  321. End Sub
  322.  
  323.  
  324. ' ************************************************
  325. ' Read a grid from a file using Input.
  326. ' Assume the "TRANSFORMED" label has already been
  327. ' read.
  328. ' ************************************************
  329. Public Sub FileInput(filenum As Integer)
  330. Dim i As Integer
  331. Dim j As Integer
  332. Dim k As Integer
  333.  
  334.     ' Get the basic information.
  335.     Input #filenum, NumCurvePts, NumTrans
  336.     
  337.     ' Allocate and read the curve array.
  338.     ReDim CurvePoints(1 To NumCurvePts)
  339.     For i = 1 To NumCurvePts
  340.         Input #filenum, _
  341.             CurvePoints(i).coord(1), _
  342.             CurvePoints(i).coord(2), _
  343.             CurvePoints(i).coord(3)
  344.         CurvePoints(i).coord(4) = 1
  345.     Next i
  346.     
  347.     ' Allocate and read the transformations.
  348.     ReDim trans(1 To NumTrans)
  349.     For i = 1 To NumTrans
  350.         For j = 1 To 4
  351.             For k = 1 To 4
  352.                 Input #filenum, trans(i).M(j, k)
  353.             Next k
  354.         Next j
  355.     Next i
  356.     
  357.     ' Create the display solid.
  358.     Transform
  359. End Sub
  360.  
  361.  
  362.